package com.pet_service.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet_service.entity.PetShareComment;
import com.pet_service.enums.AuditStatus;
import com.pet_service.result.Result;
import com.pet_service.service.PetShareCommentService;
import com.pet_service.vo.PageBean;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

/**
 * @author 邹全伸
 * @version 1.0
 * @date 2025-10-02 14:15
 */
@RestController
@Slf4j
@RequestMapping("/audit")
@Tag(name = "审核接口")
public class AuditController {
    @Autowired
    private PetShareCommentService petShareCommentService;

    //分页查询评论列表
    @PostMapping("/comment/list")
    @Operation(summary = "分页查询评论列表")
    @PreAuthorize("hasAuthority('customer:review:view')")
    public Result<IPage<PetShareComment>> list(@RequestBody PageBean pageBean) {
        Page<PetShareComment> page = new Page<>(pageBean.getPageNum(), pageBean.getPageSize());
        QueryWrapper<PetShareComment> wrapper = new QueryWrapper<>();
        if (pageBean.getQuery() != null && !pageBean.getQuery().isEmpty()) {
            wrapper.eq("audit_status", AuditStatus.valueOf(pageBean.getQuery()));
        }
        IPage<PetShareComment> iPage = petShareCommentService.page(page, wrapper);
        return Result.ok(iPage);
    }

    //审核评论：通过/拒绝
    @PostMapping("/comment/audit")
    @Operation(summary = "审核评论")
    @PreAuthorize("hasAuthority('customer:review:audit')")
    public Result audit(@RequestBody PetShareComment comment) {
        if (comment.getId() == null || comment.getAudit_status() == null) {
            return Result.fail("参数错误");
        }

        boolean success = petShareCommentService.updateById(comment);
        if (!success) {
            return Result.fail("审核失败");
        }
        // 根据实际状态返回不同提示
        String msg = "审核状态：" + comment.getAudit_status().getDesc();
        return Result.ok(msg);
    }

    //删除评价(物理删除)
    @DeleteMapping("/comment/{id}")
    @Operation(summary = "物理删除评论")
    @PreAuthorize("hasAuthority('customer:review:delete')")
    public Result delete(@PathVariable Integer id) {
        int result = petShareCommentService.deletePhysicalById(id);
        return result > 0 ? Result.ok("删除成功") : Result.fail("删除失败");
    }

}
